/*
* jQuery pager plugin
* Version 1.0 (12/22/2008)
* @requires jQuery v1.2.6 or later
*
* Example at: http://jonpauldavies.github.com/JQuery/Pager/PagerDemo.html
*
* Copyright (c) 2008-2009 Jon Paul Davies
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
* 
* Read the related blog post and contact the author at http://www.j-dee.com/2008/12/22/jquery-pager-plugin/
*
* This version is far from perfect and doesn't manage it's own state, therefore contributions are more than welcome!
*
* Usage: .pager({ pagenumber: 1, pagecount: 15, buttonClickCallback: PagerClickTest });
*
* Where pagenumber is the visible page number
*       pagecount is the total number of pages to display
*       buttonClickCallback is the method to fire when a pager button is clicked.
*
* buttonClickCallback signiture is PagerClickTest = function(pageclickednumber) 
* Where pageclickednumber is the number of the page clicked in the control.
*
* The included Pager.CSS file is a dependancy but can obviously tweaked to your wishes
* Tested in IE6 IE7 Firefox & Safari. Any browser strangeness, please report.
* 
* modified by leaves@2012-09-01
*/
(function($) {

    $.fn.pager = function(options) {

        var opts = $.extend({}, $.fn.pager.defaults, options);
        
        function urlCallback(destPage, destSize, pagecount){
        	if(destPage>pagecount)
        		return;
        	var url = window.location.href;
        	if(url.indexOf("?")>0){
        		if(url.indexOf("pager.pageNo")>0){
        			url = url.substring(0, url.lastIndexOf("pager.pageNo=")-1);
        			
        		}
        		if(url.indexOf("?")>0)
        			url +="&pager.pageNo="+destPage;
        		else
        			url +="?pager.pageNo="+destPage;
        	}else
        		url+="?pager.pageNo="+destPage;
        	if(destSize!=20){
    			url+="&pager.pageSize="+destSize;
    		}
    		window.location.href = url;
        }
        
        function formCallback(destPage, destSize, pagecount){
        	if(destPage>pagecount)
        		return;
        	var form = $("#"+opts.formId);
        	form.find(":hidden[name='pager.pageNo']").length>0?form.find(":hidden[name='pager.pageNo']"):form.append($("<input type='hidden' name='pager.pageNo'>"));
        	form.find(":hidden[name='pager.pageNo']").val(destPage);
        	if(destSize != 20){
        		form.find(":hidden[name='pager.pageSize']").length>0?form.find(":hidden[name='pager.pageSize']"):form.append($("<input type='hidden' name='pager.pageSize'>"));
            	form.find(":hidden[name='pager.pageSize']").val(destSize);
        	}
        	form.submit();
        }

        return this.each(function() {

        // empty out the destination element and then render out the pager with the supplied options
            $(this).empty();
            $(this).addClass("page");
            if(opts.submitType == "url")
            	opts.buttonClickCallback = urlCallback;
            if(opts.submitType == "form")
            	opts.buttonClickCallback = formCallback;
            renderpager($(this), parseInt(opts.pagenumber), parseInt(opts.pagecount), parseInt(opts.pagesize), opts.buttonClickCallback)
            // specify correct cursor activity 
            $('.page a').mouseover(function() { document.body.style.cursor = "pointer"; }).mouseout(function() { document.body.style.cursor = "auto"; });
        });
    };

    // render and return the pager with the supplied options
    function renderpager($pager, pagenumber, pagecount, pagesize, buttonClickCallback) {

        // setup $pager to hold render
        //var $pager = $(this);

        // add in the previous and next buttons
        $pager.append(renderButton('首页', pagenumber, pagecount, buttonClickCallback)).append(renderButton('前一页', pagenumber, pagecount, buttonClickCallback));

        // pager currently only handles 10 viewable pages ( could be easily parameterized, maybe in next version ) so handle edge cases
        var startPoint = 1;
        var endPoint = 9;

        if (pagenumber > 4) {
            startPoint = pagenumber - 4;
            endPoint = pagenumber + 4;
        }

        if (endPoint > pagecount) {
            startPoint = pagecount - 8;
            endPoint = pagecount;
        }

        if (startPoint < 1) {
            startPoint = 1;
        }
        
        // loop thru visible pages and render buttons
        for (var page = startPoint; page <= endPoint; page++) {

            var currentButton = $('<a>' + (page) + '</a>');

            page == pagenumber ? currentButton.addClass('now') : currentButton.click(function() { buttonClickCallback(this.firstChild.data, $("#pagerSizeSelect").val(), pagecount); });
            currentButton.appendTo($pager);
        }

        // render in the next and last buttons before returning the whole rendered control back.
        $pager.append(renderButton('后一页', pagenumber, pagecount, buttonClickCallback)).append(renderButton('未页', pagenumber, pagecount, buttonClickCallback));
        //pageSize select
        var sizeSelect = $("<select id='pagerSizeSelect'>");
        sizeSelect.append($("<option value='10'>10</option>"));
        sizeSelect.append($("<option value='20'>20</option>"));
        sizeSelect.append($("<option value='50'>50</option>"));
        sizeSelect.append($("<option value='100'>100</option>"));
        sizeSelect.val(pagesize);
        sizeSelect.change(function(){buttonClickCallback(1, sizeSelect.val(), pagecount)});
        $pager.append(sizeSelect);
        
        $pager.append($("<input id='nomalpagernumber' type='text' style='width:40px;'>"));
        $pager.append($('<a class="m_r_bnt1" href="#" style="padding:0px;"><input id="nomalButton" type="button" value="跳转" style="border:0 none;"></a>'));
        $("#nomalButton").click(function(){
        	if($("#nomalpagernumber").val()==''){
        		return false;
        	}
        	//判断输入的页码是否是数字
        	if (!/^\d+$/.test($("#nomalpagernumber").val())) {
        		$("#nomalpagernumber").val('');
        		return false;
        	}
        	if($("#nomalpagernumber").val()>pagecount){
        		$("#nomalpagernumber").val('');
        		return false;
        	}
        	buttonClickCallback($("#nomalpagernumber").val(), $("#pagerSizeSelect").val(), pagecount);
        	}
        );
        return $pager;
    }

    // renders and returns a 'specialized' button, ie 'next', 'previous' etc. rather than a page number button
    function renderButton(buttonLabel, pagenumber, pagecount, buttonClickCallback) {

        var $Button = $('<a class="pagea">' + buttonLabel + '</a>');

        var destPage = 1;

        // work out destination page for required button type
        switch (buttonLabel) {
            case "首页":
                destPage = 1;
                break;
            case "前一页":
                destPage = pagenumber - 1;
                break;
            case "后一页":
                destPage = pagenumber + 1;
                break;
            case "未页":
                destPage = pagecount;
                break;
        }

        // disable and 'grey' out buttons if not needed.
        if (buttonLabel == "首页" || buttonLabel == "前一页") {
            pagenumber <= 1 ? $Button.addClass('pgEmpty') : $Button.click(function() { buttonClickCallback(destPage, $("#pagerSizeSelect").val(), pagecount); });
        }
        else {
            pagenumber >= pagecount ? $Button.addClass('pgEmpty') : $Button.click(function() { buttonClickCallback(destPage, $("#pagerSizeSelect").val(), pagecount); });
        }

        return $Button;
    }

    // pager defaults. hardly worth bothering with in this case but used as placeholder for expansion in the next version
    $.fn.pager.defaults = {
        pagenumber: 1,
        pagesize: 20,
        pagecount: 1,
        submitType:"url", //url, form
        formId:""
    };

})(jQuery);




